import {
	defineStore
} from 'pinia';

export const USER = "USER_INFO"

const user = uni.getStorageSync(USER)

export const useUserStore = defineStore('user', {
	state: () => {
		return {
			id: user?.id || -1, // -1第次一使用、0游客、1员工、2管理员
			phone: user?.phone || "",
			openId: user?.openId || "",
			role: user?.role || 0,
		};
	},
	actions: {
		setUser(user) {
			console.log("setUser", user);
			this.id = user.id
			this.phone = user.phone
			this.openId = user.openId
			this.role = user.role
		},
		saveUser(user) {
			this.setUser(user)
			uni.setStorageSync(USER, user)
		},
		checkUser() {
			// 第一次登录没有缓存
			if (this.user?.id > -1) {
				return true;
			}
			// 先从本地获取
			const user = uni.getStorageSync(USER)

			if (user) {
				this.setUser(user)
				return true;
			}
			return false
		},
	},
});